import glob
import os
import signal
import scipy.io.wavfile as wav


def writeInvalidDataPath(dirPath, vocabPath):
    invalidDir = 'InvalidData'
    if not os.path.exists(invalidDir):
        os.makedirs(invalidDir)
    dirPath = dirPath.replace('/', os.sep)
    temp = dirPath.split(os.sep)
    savePath = os.path.join(invalidDir, f'{temp[-1]}.txt')
    saveVocab = os.path.join(invalidDir, f'{temp[-1]}_vocab.txt')

    allPath = []
    invalidWord = set()
    searchPath = os.path.join(dirPath, '*')
    for path in glob.glob(searchPath):
        allPath.append(path)

    with open(savePath, 'w') as f:
        for filePath in allPath:
            fs, signalWav = wav.read(filePath)
            if max(signalWav) == 0:
                f.write(f'{filePath}\n')
                temp = filePath.split(os.sep)
                word = temp[-1].split('_')[0]
                invalidWord.add(word)

    with open(saveVocab, 'w') as f:
        for word in invalidWord:
            f.write(f'{word}\n')

    sortVocabIndex(saveVocab, vocabPath)

def sortVocabIndex(missVocabPath, vocabPath):
    allVocab = open(vocabPath, 'r').read().split('\n')
    allVocab = list(filter(lambda x:x!='', allVocab))
    missVocab = open(missVocabPath, 'r').read().split('\n')
    missVocab = list(filter(lambda x:x!='', missVocab))
    vocabDict = {}
    for i in range(len(allVocab)):
        sinVocab = allVocab[i]
        if sinVocab in missVocab:
            vocabDict[i+1] = sinVocab
    with open(missVocabPath, 'w') as f:
        for key in vocabDict.keys():
            f.write(f'{key} {vocabDict[key]}\n')




dirPath = 'F:\\dataset\\final_data\\ultra\\17'
vocabPath = 'word_mod50.txt'
writeInvalidDataPath(dirPath, vocabPath)

